package monster.com.cvh.adapter;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import me.shaohui.bottomdialog.BaseBottomDialog;
import monster.com.cvh.R;
import monster.com.cvh.activity.LoginActivity;
import monster.com.cvh.bean.CommentDetailsBean;
import monster.com.cvh.bean.MessageEvent;
import monster.com.cvh.bean.PostBean;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.ToastUtils;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: monster
 * @data: 2018/4/3
 * @function: 二级三级评论adapter
 */
public class CommentMoreAdapter extends BaseQuickAdapter<CommentDetailsBean.DataBean.SubCommentsBean, BaseViewHolder> {

    private boolean is_praise;
    private BaseBottomDialog mDialog;
    private FragmentManager fragmentManager;


    public CommentMoreAdapter(int layoutResId, @Nullable List<CommentDetailsBean.DataBean.SubCommentsBean> data, FragmentManager fragmentManager) {
        super(layoutResId, data);
        this.fragmentManager = fragmentManager;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final CommentDetailsBean.DataBean.SubCommentsBean item) {
        helper.setText(R.id.tv_item_comment_more_name, item.getUser_name());
        final LinearLayout mLlItemCommentMore = helper.getView(R.id.ll_item_comment_more);
        helper.setText(R.id.tv_item_comment_more_priase, item.getPraise_count() + "");
        helper.setText(R.id.item_comment_more_content, item.getContent());
        final ImageView mIvPraise = helper.getView(R.id.iv_item_comment_more_priase);
        LinearLayout mLlPraise = helper.getView(R.id.ll_item_comment_more_details);
        final TextView mTvDetele = helper.getView(R.id.tv_item_comment_more_delete);
        if (item.getReceiver_id() == -1){
            //表示评论二级的
            mLlItemCommentMore.setVisibility(View.GONE);
        }else {
            mLlItemCommentMore.setVisibility(View.VISIBLE);
        }
        helper.setText(R.id.item_comment_more_second_user,item.getReceiver_name());


        /**
         * 点赞操作
         */
        is_praise = item.isIs_praise();
        if (String.valueOf(item.getUser_id()).equals(SPUtils.getInt(mContext, "id", -1))) {
            //当前用户，隐藏点赞
            mLlPraise.setVisibility(View.GONE);
            mTvDetele.setVisibility(View.VISIBLE);
            /**
             * 删除评论
             */
            mTvDetele.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new AlertDialog.Builder(mContext).setTitle("删除本条").setMessage("确认要删除本条数据吗？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            OkGo.<String>delete(MyConstant.DELETE_COMMENT).params("comment_id",item.getId()).params("token", SPUtils.getString(mContext, "token", ""))
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onSuccess(String s, Call call, Response response) {
                                            Gson gson = new Gson();
                                            PostBean deleteBean = gson.fromJson(s, PostBean.class);
                                            switch (deleteBean.getCode()) {
                                                case 1:
                                                    //删除成功
                                                    EventBus.getDefault().post(new MessageEvent("adapter通知我更新ui了"));
                                                    ToastUtils.showToastLong("删除成功");
                                                    break;
                                            }
                                        }
                                    });
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    }).show();

                }
            });
        } else {
            //不是当前用户,显示赞
            mTvDetele.setVisibility(View.GONE);
            mLlPraise.setVisibility(View.VISIBLE);
            if (is_praise) {
                //已经点赞
                mIvPraise.setImageResource(R.mipmap.information_praise_after);
                mIvPraise.setEnabled(false);

            } else {
                //未点赞
                mIvPraise.setImageResource(R.mipmap.information_praise_before);
                mIvPraise.setEnabled(true);
                /**
                 * 点赞qingq
                 */
                mLlPraise.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (SPUtils.getString(mContext,"token","").equals("") || SPUtils.getString(mContext,"token","") == null ){
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                        }else {
                            final int addPraise = item.getPraise_count() + 1;
                            Log.i(TAG, "onClick: " + addPraise + "position:" + helper.getLayoutPosition());
                            helper.setText(R.id.tv_item_comment_more_priase, addPraise + "");
                            OkGo.<String>post(MyConstant.MAKE_PRAISE).params("token", SPUtils.getString(mContext, "token", ""))
                                    .params("is_praise", true).params("comment_id", item.getId()).execute(new StringCallback() {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {
                                    Gson gosn = new Gson();
                                    PostBean bean = gosn.fromJson(s, PostBean.class);
                                    if (bean.getCode() == 1) {
                                        mIvPraise.setImageResource(R.mipmap.information_praise_after);
                                        mIvPraise.setEnabled(false);
                                    }

                                }
                            });
                        }
                    }
                });


            }
        }
    }

}
